package cn.znzz.cjm.org.service.impl;

import cn.znzz.cjm.basic.service.impl.BaseServiceImpl;
import cn.znzz.cjm.basic.util.MD5Utils;
import cn.znzz.cjm.basic.util.StrUtils;
import cn.znzz.cjm.org.domain.Employee;
import cn.znzz.cjm.org.mapper.EmployeeMapper;
import cn.znzz.cjm.org.service.IEmployeeService;
import cn.znzz.cjm.user.domain.Logininfo;
import cn.znzz.cjm.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 员工业务层实现
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService{
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    //基本的crud实现被抽取


    //需要级联处理logininfo信息
    @Override
    public void add(Employee employee) {
        InitEmployee(employee);//设置Employee必要参数
        Logininfo logininfo=employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//一定要设置
        return logininfo;
    }

    private void InitEmployee(Employee employee) {
        //生成颜值
        String salt = StrUtils.getComplexRandomString(32);
        //加密密码
        String md5Password = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Password);
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }
}

























